/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2021-10-18
 * Time: 10:30
 */
public class ComparesStrings {
    //法一：
    //构建StrBuffer或者StrBuilder,相当于使用栈来解决问题
    public static boolean backspaceCompare(String s, String t){
        return build(s).equals(build(t));

    }
    public static  String build(String str){
        StringBuilder ret=new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char ch=str.charAt(i);
            if(ch!='#'){
                ret.append(ch);//相当于栈的push
            }else{
                if(ret.length()>0) {//当重建字符串里有字符的时候
                    ret.deleteCharAt(ret.length() - 1);
                }
            }
        }
        return ret.toString();//又换成String
    }

    //法二：
    /*
    public static boolean backspaceCompare(String s, String t){
    StringBuilder ssb = new StringBuilder(); // 模拟栈
    StringBuilder tsb = new StringBuilder(); // 模拟栈
    // 分别处理两个 String
        for (char c : s.toCharArray()) {
        if (c != '#') {
            ssb.append(c); // 模拟入栈
        } else if (ssb.length() > 0){ // 栈非空才能弹栈
            ssb.deleteCharAt(ssb.length() - 1); // 模拟弹栈
        }
    }
        for (char c : t.toCharArray()) {
        if (c != '#') {
            tsb.append(c); // 模拟入栈
        } else if (tsb.length() > 0){ // 栈非空才能弹栈
            tsb.deleteCharAt(tsb.length() - 1); // 模拟弹栈
        }
    }
        return ssb.toString().equals(tsb.toString());
        }
     */

    public static void main(String[] args) {
        String S="a##b";
        String T="#ab#";
        System.out.println(backspaceCompare(S, T));
    }
}
